﻿using System;
using System.Diagnostics;
using Microsoft.Build.Utilities;
using Microsoft.Build.Framework;
using Microsoft.TeamFoundation.Build.Client;
using Microsoft.TeamFoundation.Build.Proxy;
using Microsoft.TeamFoundation.Client;
using BuildStepStatus = Microsoft.TeamFoundation.Build.Client.BuildStepStatus;

namespace TeamDeploy.Tasks
{
    abstract public class TaskBase : Task
    {

        private string teamFoundationServerUrl = "";
        private string buildUri = "";
        protected bool ignoreExitCode = false;
        private TeamFoundationServer tfs;
        private IBuildServer buildServer;
        private IBuildDetail buildDetail;
        protected IBuildStep buildStep;
        private bool includeBuildStep = false;



        /// <summary>
        /// Initializes the build step.
        /// </summary>
        /// <param name="stepName">Displays in Build Steps</param>
        /// <param name="message">Message</param>
        protected void InitializeBuildStep(string stepName, string message)
        {
            if (BuildUri.Length > 0 && TeamFoundationServerUrl.Length > 0)
            {
                includeBuildStep = true;
                buildStep = InformationNodeConverters.AddBuildStep(BuildDetail, stepName, message);
            }
        }


        /// <summary>
        /// The Url of the Team Foundation Server.
        /// </summary>
        public string TeamFoundationServerUrl
        {
            get
            {
                return teamFoundationServerUrl;
            }
            set
            {
                teamFoundationServerUrl = value;
            }
        }

        /// <summary>
        /// Lazy init property that gives access to the TF Server specified by TeamFoundationServerUrl.
        /// </summary>
        protected TeamFoundationServer Tfs
        {
            get
            {
                if (tfs == null)
                {
                    if (String.IsNullOrEmpty(TeamFoundationServerUrl))
                    {
                        // Throw some exception.
                    }
                    tfs = TeamFoundationServerFactory.GetServer(TeamFoundationServerUrl);
                }
                return tfs;
            }
        }


        /// <summary>
        /// The Uri of the Build for which this task is executing.
        /// </summary>
        public string BuildUri
        {
            get
            {
                return buildUri;
            }
            set
            {
                buildUri = value;
            }
        }

        public string IgnoreExitCode
        {
            get
            {
                return ignoreExitCode.ToString();
            }
            set
            {
                ignoreExitCode = bool.Parse(value);
            }
        }
        /// <summary>
        /// Lazy init property that gives access to the BuildStore service of the TF Server.
        /// </summary>
        protected IBuildServer BuildServer
        {
            get
            {
                if (buildServer == null)
                {
                    buildServer = (IBuildServer)Tfs.GetService(typeof(IBuildServer));
                }
                return buildServer;
            }
        }

        /// <summary>
        /// Lazy init property that gives access to the Build specified by BuildUri.
        /// </summary>
        protected IBuildDetail BuildDetail
        {
            get
            {
                if (buildDetail == null)
                {
                    buildDetail = (IBuildDetail)BuildServer.GetBuild(new Uri(BuildUri), null, QueryOptions.None);
                }
                return buildDetail;
            }
        }

        protected void UpdateBuildStep(bool result)
        {
            if(!includeBuildStep)
                return;

            buildStep.Status = result ? BuildStepStatus.Succeeded : BuildStepStatus.Failed;
            buildStep.FinishTime = DateTime.Now;
            buildStep.Save();
        }
    }
}
